package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherDetailDataBean;
import com.fancy.learncenter.bean.CheckCreatClassDataBean;
import com.fancy.learncenter.bean.ListStudentDataBean;
import com.fancy.learncenter.bean.TeachersBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonStudentAdapter;
import com.fancy.learncenter.ui.adapter.CartoonTeacherAdapter;
import com.fancy.learncenter.ui.view.CartoonTeacherPopupWindow;
import com.fancy.learncenter.ui.view.PromptPopupWindow;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.fancy.learncenter.ui.activity.CartoonTeacherClassDetailActivity.CLASS_STUDENT_NUM_KEY;

public class CartoonTeacherSudentActivity extends BaseActivity {


    String clasName;
    String classId;


    CartoonStudentAdapter cartoonStudentAdapter;

    CartoonTeacherAdapter cartoonTeacherAdapter;

    ArrayList<TeachersBean> itemTeacherDatas;

    ArrayList<ListStudentDataBean.StudentsBean> itemStudentDatas;


    int studentNum = 0;


    @Bind(R.id.teacherRecyclerView)
    RecyclerView teacherRecyclerView;
    @Bind(R.id.student_recycleview)
    RecyclerView studentRecycleview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_sudent, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("班级详情");


        clasName = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_NAME_KEY);
        classId = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY);

        limitNum = getIntent().getIntExtra(CLASS_STUDENT_NUM_KEY, 100);

        initToolbar(clasName);

        itemTeacherDatas = new ArrayList<>();

        initTeacherRecycle();

        initStudentRecycle();
    }

    private void initTeacherRecycle() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        teacherRecyclerView.setLayoutManager(linearLayoutManager);


        cartoonTeacherAdapter = new CartoonTeacherAdapter(this, itemTeacherDatas);
        cartoonTeacherAdapter.setClickListener(new CartoonTeacherAdapter.ClickCallBack() {
            @Override
            public void onclickCallBack(TeachersBean item) {

                if (cartoonTeacherPopupWindow == null) {
                    cartoonTeacherPopupWindow = new CartoonTeacherPopupWindow(CartoonTeacherSudentActivity.this);
                }
                cartoonTeacherPopupWindow.setData(item);
                cartoonTeacherPopupWindow.showPopupWindow();
            }

            @Override
            public void onlongclickCallBack(TeachersBean item, int position) {
                delteClass(item.getName(), item.getId(), position);
            }
        });


        teacherRecyclerView.setAdapter(cartoonTeacherAdapter);
    }

    PromptPopupWindow promptDialog;

    private void delteClass(String teacherName, final String teachid, final int position) {


        if (promptDialog == null) {
            promptDialog = new PromptPopupWindow(this, teacherRecyclerView);
            promptDialog.setCallBack(new PromptPopupWindow.CallBack() {
                @Override
                public void confirm() {
                    deletClassData(teachid, position);
                }

                @Override
                public void cancle() {

                }
            });
        }
        promptDialog.setPromptTitle("是否删除" + teacherName + "老师？");
        promptDialog.showPopupWindow();

    }

    private void deletClassData(String teacherId, final int position) {

        Map<String, Object> map = new HashMap<>();

        map.put("classId", classId);
        map.put("teacherId", teacherId);
        HttpMehtod.getInstance().delTeacherFromClas(map, new CustomNetSubscriber(this, false) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CheckCreatClassDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CheckCreatClassDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CheckCreatClassDataBean>() {
                    @Override
                    public void success(CheckCreatClassDataBean dataBean) {
                        ToastUtil.show("删除成功");
                        cartoonTeacherAdapter.itemDatas.remove(position);
                        cartoonTeacherAdapter.notifyDataSetChanged();
//                        if (dataBean.isResult()) {
//
//                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

        });
    }


    CartoonTeacherPopupWindow cartoonTeacherPopupWindow;

    private void initStudentRecycle() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        studentRecycleview.setLayoutManager(linearLayoutManager);

        itemStudentDatas = new ArrayList<>();
        cartoonStudentAdapter = new CartoonStudentAdapter(this, itemStudentDatas, classId);


        studentRecycleview.setAdapter(cartoonStudentAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();

        getTeacherList();

        getStudentList();
    }

    private void getTeacherList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);

        HttpMehtod.getInstance().getTeacherList(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CartoonTeacherDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherDetailDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherDetailDataBean>() {
                    @Override
                    public void success(CartoonTeacherDetailDataBean dataBean) {
                        List<TeachersBean> itemDatas = dataBean.getTeachers();

                        TeachersBean teachersBean = new TeachersBean();
                        teachersBean.setImageUrl("res://com.fancy.learncenter/" + R.mipmap.default_add);
                        teachersBean.setName("添加老师");
                        teachersBean.setId(classId);
                        teachersBean.setStatus("1");
                        itemDatas.add(teachersBean);

                        cartoonTeacherAdapter.notifyDataSetChanged(itemDatas);
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }


    private void getStudentList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);

        HttpMehtod.getInstance().getStudentList(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<ListStudentDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), ListStudentDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<ListStudentDataBean>() {
                    @Override
                    public void success(ListStudentDataBean dataBean) {

                        studentNum = dataBean.getStudents().size();
                        cartoonStudentAdapter.notifyDataSetChanged(dataBean.getStudents());
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

    int limitNum = 100;

    @OnClick(R.id.add_student)
    public void onViewClicked() {

        if (limitNum > studentNum) {
            Intent intent = new Intent(this, CartoonQueryTeacherActivity.class);
            intent.putExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY, classId);
            startActivity(intent);
        } else {
            ToastUtil.show("学生人员已满");
        }

    }
}
